//
//  QuestionView.swift
//  SwiftDemo
//
//  Created by DYD on 2018/12/6.
//  Copyright © 2018 DYD. All rights reserved.
//

import UIKit
import SnapKit

class QuestionView: UIView {

    lazy var tableView: UITableView = {
        let view = UITableView(frame: CGRect.zero, style: UITableView.Style.plain)
        view.delegate = self
        view.dataSource = self
        view.backgroundColor = CDColor.grayBackgroundColorA
        view.register(QuestionChooseView.self, forCellReuseIdentifier: "QuestionChooseView")
        view.tableFooterView = UIView()
        return view
    }()
    
    lazy var dataSource = [QuestionModel]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = CDColor.grayBackgroundColorA
        addSubview(tableView)
        tableView.snp.remakeConstraints { (make) in
            make.edges.equalTo(0)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension QuestionView: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "QuestionChooseView", for: indexPath) as! QuestionChooseView
        cell.config(dataSource[indexPath.row])
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return dataSource[indexPath.row].viewHeight
    }
}
